<?php

namespace IDock\Insurance\LIHI\Request;

use Exception;
use IDock\Insurance\LIHI\LIHIEnums;
use IDock\Insurance\LIHI\Response\PremiumInquiryResponse;
use IDock\Visitor\Premium\PremiumChargingPileVisitor;
use IDock\Visitor\Premium\PremiumCoverageVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumRelationVisitor;

class VehicleConfirmRequest extends AllLIHIRequest
{
    public $route = 'calculate/comfirmVehicle';

    protected $response = PremiumInquiryResponse::class;

    protected $urlTip = '车型确认-再报价';

    private $C16002 = false;

    private $C16003 = false;

    /*** @var  PremiumInquiryVisitor $visitor */
    protected $visitor;

    /**
     * @throws Exception
     */
    public function generate(): array
    {
        $this->agreementCode = $this->visitor->getExtInfoVisitor()->argc['agreementNo'] ?? '';
        $this->recordCode = $this->visitor->getExtInfoVisitor()->argc['recordCode'] ?? '';

        $this->flowId = $this->visitor->getExtInfoVisitor()->argc['flowId'] ?? '';

        $data = [
            'tYbProposalQueryVehicleDto' => $this->mainData(),
            'tprpTitemKindListDto' => $this->getCoverageList(),
        ];

        if ($chargingList = $this->chargingList()) {
            $data['tprpTitemKindSubListDto'] = $chargingList;
        }

        return $data;
    }


    /**
     * 车辆信息
     * @return array
     */
    private function mainData(): array
    {
        return $this->visitor->vehiclePrinter->item;
    }

    /**
     * 险种信息
     * @return array
     */
    private function getCoverageList(): array
    {
        $list = [];
        $no = 1;

        $coverageList = $this->visitor->getCoverageVisitorList();
        $coverageList = array_combine(array_column($coverageList, 'coverageCode'), $coverageList);

        foreach ($coverageList as $coverage) {

            /*** @var PremiumCoverageVisitor $coverage */

            //交强险去除
            if ($coverage->coverageCode == 'C01001') {
                continue;
            }
            if ($coverage->coverageCode == 'C16002') {
                $this->C16002 = true;
                continue;
            }
            if ($coverage->coverageCode == 'C16003') {
                $this->C16003 = true;
                continue;
            }
            $one = [
                'itemKindNo' => (string)$no,
                'kindCodeMain' => LIHIEnums::$coverageCode[$coverage->coverageCode],
                'kindNameMain' => LIHIEnums::$coverageName[$coverage->coverageCode],
                'quantityMain' => (string)intval($coverage->quantity),
                'amountMain' => $coverage->amount,
            ];

            //乘客险
            if (in_array($coverage->coverageCode, ['C05001', 'C15001',])) {
                $one['quantityMain'] = strval($this->visitor->getVehicleVisitor()->vehicleSeat - 1);
                $one['unitAmountMain'] = $coverage->unitAmount;
            }

            //乘客险保外
            if (in_array($coverage->coverageCode, ['C05002', 'C15002'])) {
                $one['quantityMain'] = strval($this->visitor->getVehicleVisitor()->vehicleSeat - 1);
                $one['unitAmountMain'] = $coverage->amount;
                $one['amountMain'] = $one['quantityMain'] * $one['unitAmountMain'];
            }
            //医疗外 乘客
            if (in_array($coverage->coverageCode, ['C05003', 'C15003'])) {
                $one['quantityMain'] = strval($this->visitor->getVehicleVisitor()->vehicleSeat - 1);
                $one['unitAmountMain'] = '';
                $one['amountMain'] = $coverage->amount;
            }

            //绝对免赔率
            if (in_array($coverage->coverageCode, ['C02006', 'C03005', 'C04004', 'C05004', 'C12006', 'C13005', 'C14004', 'C15004'])) {
                $one['quantityMain'] = $coverage->deductionRate;
            }
            //单份险种费用
            if (in_array($coverage->coverageCode, ['C02003', 'C12003', 'C16003', 'C05004', 'C15004'])) {
                $one['unitAmountMain'] = $coverage->unitAmount;
                $one['amountMain'] = (string)round((floatval($one['quantityMain']) * floatval($one['unitAmountMain'])), 2);
            }

            //司机险
            //if (in_array($coverage->coverageCode, ['C04001', 'C04002', 'C04003', 'C14001', 'C14002', 'C14003'])) {
            //
            //}

            $no += 1;

            $list[] = $one;
        }
//        //非车
//        if(!empty($this->visitor->getNonCarInquiryVisitor()->getNonCarList())){
//            /**@var NonCarProductVisitor $noCarPlan */
//            $noCarPlan = $this->visitor->getNonCarInquiryVisitor()->getNonCarList()[0] ?? [];
//            $amount = 0;
//            foreach ($noCarPlan->subCoverage as $val){
//                /**@var NonCarSubCoveragePrinter $val */
//                $amount += floatval($val->amount);
//            }
//            $list[] = [
//                'itemkindNo' => count($list) + 1,
//                'kindCode' => $noCarPlan->productCode,
//                'quanTity' => intval($noCarPlan->insuredNum),
//                'unitAmount' => $noCarPlan->premium,
//                'amount' => floatval($noCarPlan->premium *  $noCarPlan->insuredNum),
//                'kindFlag' => '1',
//            ];
//        }
        return $list;
    }

    /**
     * 充电桩
     * @return array
     */
    private function chargingList(): array
    {
        if (!$this->C16003 and !$this->C16002) {
            return [];
        }
        $list = [];
        $no = 1;
        foreach ($this->visitor->getChargingPileVisitorList() as $visitor) {
            /*** @var PremiumChargingPileVisitor $visitor */
            //C1
            if ($this->C16002) {
                $kindList = [
                    'itemkindNo' => (string)$no,
                    'kindCodeMain' => 'C203',
                    'kindNameMain' => LIHIEnums::$coverageName['C16002'],
                    'unitAmountMain' => $visitor->lossAmount,
                    'amountMain' => $visitor->lossAmount,
                    'chargingPostType' => $visitor->chargingPileType,
                    'chargingPostCode' => $visitor->chargingPileCode,
                    'chargingPostAddress' => $visitor->chargingPileAddress,
                    'chargingPostKind' => LIHIEnums::$chargingPileKind[$visitor->chargingPileKind],
                    'chargingPostAddressType' => LIHIEnums::$chargingPileAddressType[$visitor->chargingPileAddressType],
                    'chargingPostYearLimit' => LIHIEnums::$chargingPileYearLimit[$visitor->chargingPileYearLimit],
                ];
                $list[] = $kindList;
                $no += 1;
            }

            //C2
            if ($this->C16003) {
                $kindList = [
                    'itemkindNo' => (string)$no,
                    'kindCodeMain' => 'C204',
                    'kindNameMain' => LIHIEnums::$coverageName['C16003'],
                    'unitAmountMain' => $visitor->liabilityAmount,
                    'amountMain' => $visitor->liabilityAmount,
                    'chargingPostType' => $visitor->chargingPileType,
                    'chargingPostCode' => $visitor->chargingPileCode,
                    'chargingPostAddress' => $visitor->chargingPileAddress,
                    'chargingPostKind' => LIHIEnums::$chargingPileKind[$visitor->chargingPileKind],
                    'chargingPostAddressType' => LIHIEnums::$chargingPileAddressType[$visitor->chargingPileAddressType],
                    'chargingPostYearLimit' => LIHIEnums::$chargingPileYearLimit[$visitor->chargingPileYearLimit],
                ];

                $list[] = $kindList;
                $no += 1;
            }
        }
        return $list;

    }
}
